from flask import Flask, render_template, request
import query
from flask import Blueprint
from MySQL import mysql_c
from flask_cors import CORS

teacher_api = Blueprint('teacher_api', __name__)
CORS(teacher_api)	# 注册CORS
# 教师登录接口
@teacher_api.route('/teacher_login', methods=['get', 'post'])
def login():
    if (request.method == 'POST'):
        username = request.json.get('username')
        password = request.json.get('password')
    else:
        username = request.values.get('username')
        password = request.values.get('password')

        if username and password:
            backinf = query.teacher_login_check(mysql_c,username,password)
            if backinf[0] == "密码正确":
                return {"msg": str(backinf[0]), "state": 1,"data":backinf[1]}
            else:
                return {"msg": str(backinf[0]), "state": -1,"data":backinf[1]}
        else:
            return {"msg": "数据不完整", "state": -1,"data":None}

# 老师注册接口
@teacher_api.route('/teacher_re', methods=['get', 'post'])
def reg():
    if (request.method == 'POST'):
        opusername = request.json.get('opusername')
        name = request.json.get('name')
        username = request.json.get('username')
        password = request.json.get('password')
        level = request.json.get('level')
    else:
        opusername = request.values.get('opusername')
        name = request.values.get('name')
        username = request.values.get('username')
        password = request.values.get('password')
        level = request.values.get('level')

        if opusername and name and username and password and level:
            backinf = query.teacher_register(mysql_c,opusername,username,name,password,level)
            if backinf == "成员插入成功":
                return {"msg": str(backinf), "state": 1}
            else:
                return {"msg": str(backinf), "state": -1}
        else:
            return {"msg": "数据不完整", "state": -1}

# 老师查看自己全部班级接口
@teacher_api.route('/teacher_all_classes', methods=['get', 'post'])
def all_classes():
    if (request.method == 'POST'):
        username = request.json.get('username')
    else:
        username = request.values.get('username')
        if username:
            backinf = query.get_teacher_classes(mysql_c,username)
            if backinf[0] == -1:
                return {"msg": "获取失败", "state": -1, "data": None}
            elif backinf[0] == 1:
                return {"msg": "获取成功", "state": 1, "data": backinf[1]}
        else:
            return {"msg": "数据不完整", "state": -1, "data": None}

#老师查看指定班级的全部学生
@teacher_api.route('/classes_student', methods=['get', 'post'])
def css():
    if (request.method == 'POST'):
        cid = request.json.get('cid')
    else:
        cid = request.values.get('cid')
        if cid:
            backinf = query.get_classes_student(mysql_c,cid)
            if backinf[0] == -1:
                return {"msg": "获取失败", "state": -1, "data": None}
            elif backinf[0] == 1:
                return {"msg": "获取成功", "state": 1, "data": backinf[1]}
        else:
            return {"msg": "数据不完整", "state": -1, "data": None}

#老师判成绩
@teacher_api.route('/give_score', methods=['get', 'post'])
def givescore():
    if (request.method == 'POST'):
        jid = request.json.get('jobid') #作业id
        score = request.json.get('score') #分数
        comment = request.json.get('comment') #评语
    else:
        jid = request.values.get('jobid')
        score = request.values.get('score')
        comment = request.values.get('comment')

    if jid:
        backinf = query.give_score(mysql_c, jid,score,comment)
        if backinf[0] == -1:
            return {"msg": "获取失败", "state": -1, "data": None}
        elif backinf[0] == 1:
            return {"msg": "获取成功", "state": 1, "data": backinf[1]}
    else:
        return {"msg": "数据不完整", "state": -1, "data": None}

#查找老师
@teacher_api.route('/teacher_search', methods=['get', 'post'])
def teachersearch():
    if (request.method == 'POST'):
        username = request.json.get('username') #老师的用户名，选填
        tid = request.json.get('tid') #老师的tid，选填
        #这两个参数只能填写其中一个，否则报错
    else:
        username = request.values.get('username')
        tid = request.values.get('tid')

    if (username or tid) is 1:
        passtype = 1
        type = 2
        if username:
            passtype = username
            type = 1
        if tid:
            passtype = tid
            type = 2
        backinf = query.teacher_search(mysql_c,passtype,type)
        if backinf[0] == -1:
            return {"msg": "获取失败", "state": -1, "data": None}
        elif backinf[0] == 1:
            return {"msg": "获取成功", "state": 1, "data": backinf[1]}
    else:
        return {"msg": "数据不完整", "state": -1, "data": None}

